package edu.uwosh.cs342.group1;

import edu.uwosh.cs342.group1.database.DbCloudHelper;
import edu.uwosh.cs342.group1.storage.CloudUser;
import android.app.*;
import android.content.*;
import android.os.Bundle;
import android.view.*;
import android.widget.*;

public class LogInActivity extends Activity {
	/** Called when the activity is first created. */
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
    }
    
    /**
     * Action when the user pressed the Ok button on login
     * Authenticates the user, indicates whether they are valid or not
     * @param v
     */
    public void okButton(View v) {
    	((ProgressBar)findViewById(R.id.loggingInBar)).setVisibility(View.VISIBLE);
		String username = ((EditText)findViewById(R.id.usernameTextBox)).getText().toString();
		String password = ((EditText)findViewById(R.id.passwordTextBox)).getText().toString();
		CloudUser user = new CloudUser(username, password);
		//Change to more logical
		if(retrieveUser(user)) {
			Intent i = new Intent(LogInActivity.this, QuizAppActivity.class);
			
			i.putExtra("User", username);
    		LogInActivity.this.startActivityForResult(i,1);
    		LogInActivity.this.finish();
		} else {
			loginErrorPrompt();
			((EditText)findViewById(R.id.passwordTextBox)).setText("");
		}
	 }
	 
    /**
     * Action when the user presses the cancel button on login
     * Backs out of the application
     * @param v
     */
	 public void cancelButton(View v) {
		 LogInActivity.this.finish();
	 }

	 /**
	  * Shows when the user is not valid or exists in the cloud database
	  */
	 public void loginErrorPrompt() {
    	((ProgressBar)findViewById(R.id.loggingInBar)).setVisibility(View.INVISIBLE);
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle("Log In Mismatch");
		alert.setMessage("Username and/or password do not match");
		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				dialog.cancel();
			}
		});
		alert.show();
		
	 }
	 
	 /**
	  * Checks the user on the cloud server
	  * @param user username and password in object
	  * @return true if valid, false if not valid
	  */
	 private boolean retrieveUser(CloudUser user) {
		 return new DbCloudHelper().AuthenticateUser(user);
	 }
	 
	 public void optionPopupButton(View v) {
		 final CharSequence[] items = { "Add New User", "Update Password" };
		 AlertDialog.Builder builder = new AlertDialog.Builder(this);
		 builder.setTitle("Options");
		 builder.setItems(items, new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				Intent i = null;
				switch (which) {
				case 0: // Add New User
					i = new Intent(LogInActivity.this, AddUserActivity.class);
					break;
				case 1: // Update Password
					i = new Intent(LogInActivity.this, UpdateUserActivity.class);
					break;
				}
	    		LogInActivity.this.startActivity(i);
	    		LogInActivity.this.finish();
				
			}
		});
		AlertDialog alert = builder.create();
		alert.show();
	 }

}
